package com.xbj.voa;

import cn.hutool.core.util.StrUtil;
import com.xbj.voa.common.config.SystemConstants;
import com.xbj.voa.db.dao.SysConfigDao;
import com.xbj.voa.db.pojo.SysConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;

@SpringBootApplication
@ServletComponentScan   //不加过滤器不会生效
public class VoaApplication {
    private static Logger LOG = LoggerFactory.getLogger(VoaApplication.class);

    @Resource
    private SysConfigDao sysConfigDao;
    @Resource
    private SystemConstants systemConstants;
    public static void main(String[] args) {
        SpringApplication.run(VoaApplication.class, args);
    }

    @PostConstruct
    public void init(){
        List<SysConfig> sysConfigs = sysConfigDao.searchAllSysConfig();
        sysConfigs.forEach(sysConfig -> {
            String key = sysConfig.getParamKey();
            key = StrUtil.toCamelCase(key);
            String value = sysConfig.getParamValue();

            try {
                Field declaredField = systemConstants.getClass().getDeclaredField(key);
                declaredField.set(systemConstants,value);
            } catch (Exception e) {
                LOG.info("从数据库获取配置信息,初始化异常");
            }
        });
    }

}
